package cn.exrick.xboot.modules.AppUpdate.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.AppUpdate.dao.AppUpdateDao;
import cn.exrick.xboot.modules.AppUpdate.entity.AppUpdate;
import cn.exrick.xboot.modules.AppUpdate.service.IAppUpdateService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author portal
 */
@Slf4j
@RestController
@Api(description = "app管理接口")
@Transactional
public class AppUpdateController {
    @Autowired
    private IAppUpdateService iAppUpdateService;
    @Autowired
    private AppUpdateDao appUpdateDao;
   /* @Autowired
    private IAppUpdateService iAppUpdateService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<AppUpdate> get(@PathVariable String id){

        AppUpdate appUpdate = iAppUpdateService.getById(id);
        return new ResultUtil<AppUpdate>().setData(appUpdate);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<AppUpdate>> getAll(){

        List<AppUpdate> list = iAppUpdateService.list();
        return new ResultUtil<List<AppUpdate>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<AppUpdate>> getByPage(@ModelAttribute PageVo page){

        IPage<AppUpdate> data = iAppUpdateService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<AppUpdate>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<AppUpdate> saveOrUpdate(@ModelAttribute AppUpdate appUpdate){

        if(iAppUpdateService.saveOrUpdate(appUpdate)){
            return new ResultUtil<AppUpdate>().setData(appUpdate);
        }
        return new ResultUtil<AppUpdate>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iAppUpdateService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }*/


    @RequestMapping(value = "/version/getVersion.ht", method = RequestMethod.GET)
    @ApiOperation(value = "APP获取版本信息，用于更新功能")
    public Result<AppUpdate> getVersion(@ModelAttribute AppUpdate appUpdate){
        //AppUpdate appUpdate = iAppUpdateService.getById("123");
        log.info(appUpdate.toString());
        AppUpdate app = appUpdateDao.findFirstByAppCodeOrderByUpdateTimeDesc(appUpdate.getAppCode());
        return new ResultUtil<AppUpdate>().setData(app);
    }

    @RequestMapping(value = "/version/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<AppUpdate> saveOrUpdate(@ModelAttribute AppUpdate appUpdate){

        if(iAppUpdateService.saveOrUpdate(appUpdate)){
            return new ResultUtil<AppUpdate>().setData(appUpdate);
        }
        return new ResultUtil<AppUpdate>().setErrorMsg("操作失败");
    }
}
